package com.splatform.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by sunfeihu on 2017/5/15.
 */
@Controller
public class HomeController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping(value = "/list")
    public String index(HttpServletRequest request) {
        Boolean isLogin = (Boolean) request.getSession().getAttribute("login");
        if (!isLogin) {
            return "nologin";
        }
        return "/list";
    }

    @GetMapping(value = "/suiulogin")
    public String login() {
        return "/login";
    }

    @GetMapping(value = "/code/{aid}")
    public String showCode(@PathVariable Long aid, Model model, HttpServletRequest request) {
        Boolean isLogin = (Boolean) request.getSession().getAttribute("login");
        if (!isLogin) {
            return "nologin";
        }
        Map<String, Object> activity = jdbcTemplate.queryForMap(" select * from activity where id = ? ", new Object[]{aid});
        Map<String, Object> gimg = jdbcTemplate.queryForMap(" select * from group_imgs where activity_id = ? and sort = ? ", new Object[]{
                aid, activity.get("now_code")
        });
        model.addAttribute("img", (String) gimg.get("path"));
        return "/show_code";
    }

    @GetMapping(value = "/activity/list")
    public String activityList(Model model,HttpServletRequest request) {
        Boolean isLogin = (Boolean) request.getSession().getAttribute("login");
        if (!isLogin) {
            return "nologin";
        }
        model.addAttribute("list", jdbcTemplate.queryForList(" select * from activity order by id desc "));
        return "/activity/list";
    }

    @GetMapping(value = "/activity/create")
    public String activityCreate() {
        return "/activity/edit";
    }

}